package com.yc.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yc.pay.constant.RedisKeyConstant;
import com.yc.pay.mapper.MchInfoMapper;
import com.yc.pay.model.MchInfo;
import com.yc.pay.service.IMchService;
import com.yc.pay.service.JedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 商户信息的接口的实现类
 */
@Service
public class MchServiceImpl implements IMchService {


    @Autowired
    private MchInfoMapper mchInfoMapper;

    @Autowired
    private ValueOperations valueOperations;


    @Override
    public MchInfo queryByMchId(String mchId) {

        //1. 先查redis
//        String mchInfoStr = jedisService.get(RedisKeyConstant.MCH_INFO_ID_KEY + mchId);
        MchInfo mchInfo = (MchInfo) valueOperations.get(RedisKeyConstant.MCH_INFO_ID_KEY + mchId);

        //2.为空查数据库
        if (ObjectUtils.isEmpty(mchInfo)){
             mchInfo =  mchInfoMapper.selectByPrimaryKey(mchId);
            System.out.println("---------mchInfo1mchInfo1mchInfo1---" + mchInfo);

//            JSONObject jsonObject = (JSONObject)JSON.toJSON(mchInfo);
            valueOperations.set(RedisKeyConstant.MCH_INFO_ID_KEY+ mchId,mchInfo);
        }
        System.out.println("---------mchInf111o---" + mchInfo);
        return mchInfo;
    }
}
